10 PRINT "What is your name?" 20 k$ = INKEY$ 30 PRINT k$ 40 LET total = 0 50 FOR counter = 0 TO 99 60 total = total + counter 70 NEXT counter 80 PRINT total
25 PRINT "Hello"
alert("Hello" + prompt("What is your name?"));
alert(
Array
.from(Array(100).keys())
.reduce(
(total, value, i) => (total + i),
0
)
);
[1,2,3,4].filter( val => (val > 2) ) [1,2,3,4].map( val => (val + val) ) [1,2,3,4].reduce( (accumulator, val) => (accumulator + val), 0, )
const obj = { a:1, b:2 };
const keys = Object.keys(obj);
const filteredKeys = keys.filter(
key => ( obj[key] > 1 )
);
const filteredObj = filteredKeys.reduce(
(accumulator, key) => {
accumulator[key] = obj[key];
return accumulator;
},
{},
);
const obj = { a:1, b:2 };
const filteredObj = Object
.keys(obj)
.filter( key => ( obj[key] > 1 ) )
.reduce(
(accumulator, key) => {
accumulator[key] = obj[key];
return accumulator;
},
{},
);
"Pure Functions"
"No Side Effects"
Immutable and Stateless.
"Currying"
R.add() and R.ap() examples
Speed
Functions are easier to test, to mock
They will be fast soon
// @flow
const getUsers = (users: any, index: number) => {
return users[id];
};
const users = [{name: 'Matthew'}];
// args wrong!
getUsers(0, users);
type User = { name: string }
type Users = Array<User>
const renderUsers = (users: Users, index: number) => {
return users[index];
};
renderUsers( {name: 'Matt', other: false }, 0);
function bigNumbersRcool(num: number) {
if (num > 10) {
return 'cool';
}
}
const result: string = bigNumbersRcool(100);
console.log(result.toString());
console.log(bigNumbersRcool(1).toString());
// TypeScript:
// error at runtime
// "Cannot read property 'toString' of undefined"
// Flow:
// error: call of method `toString`.
// Method cannot be called on possibly null value
(credit)
Use typing on long term or big projects